namespace Umbraco.Ecommerce.StoreAdminstration
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	public class OrderDataGridViewWithPager : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.LinkButton FirstPage;
		protected System.Web.UI.WebControls.LinkButton PreviousPage;
		protected System.Web.UI.WebControls.Label CurrentPage;
		protected System.Web.UI.WebControls.Label TotalPages;
		protected System.Web.UI.WebControls.LinkButton NextPage;
		protected System.Web.UI.WebControls.LinkButton LastPage;
		protected System.Web.UI.WebControls.DropDownList ItemsOnPage;
		protected umbraco.uicontrols.Pane Pane2;
		protected umbraco.uicontrols.Pane Pane3;
		protected umbraco.uicontrols.Pane Pane4;
		protected int _currentPageNumber = 1;

		private BusinessLogic.Order.Status _status = BusinessLogic.Order.Status.NotConfirmed;

        public BusinessLogic.Order.Status Status
        {
			get {
				return _status;
			}
			set {
				_status = value;
			}
		}

		private int DisplayItemsCount {
			get {
				if (Session["DisplayItemsCount"] == null) 
					return 10;
				return (int)Session["DisplayItemsCount"];
			} set {
				Session["DisplayItemsCount"] = value;
		  }
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			DataGrid1.PageSize = DisplayItemsCount;
			if (Status == BusinessLogic.Order.Status.Confirmed) {
				DataGrid1.Columns[4].Visible= true;
				DataGrid1.Columns[5].Visible= true;
			}
			if (!IsPostBack) {
				BindData();
				try 
				{
					ItemsOnPage.SelectedValue = DisplayItemsCount.ToString();
				}
				catch {}
			}

			if (DataGrid1.Items.Count > 0) 
				{
					Pane2.Visible = false;
				} 
			else {
				Pane3.Visible = false;
				Pane4.Visible = false;
			}
		}

		protected void NavigationLink_Click ( Object sender, CommandEventArgs e )
		{
			switch ( e.CommandName )
			{
				case "First":
					_currentPageNumber = 1;
					break;
				case "Last":
					_currentPageNumber = int.Parse ( TotalPages.Text );
					break;
				case "Next":
					_currentPageNumber = int.Parse ( CurrentPage.Text ) + 1;
					break;
				case "Prev":
					_currentPageNumber = int.Parse ( CurrentPage.Text ) - 1;
					break;
			}

			BindData();
		}

		public void BindData()
		{
			DataGrid1.DataSource = BusinessLogic.Order.GetOrdersByPageAndStatus(DataGrid1.PageSize,_currentPageNumber,_status);
			DataGrid1.DataBind();
			CurrentPage.Text = _currentPageNumber.ToString();
			Double _totalPages = 1;

			int _totalRecords = BusinessLogic.Order.OrderCountByStatus(Status);
			_totalPages = double.Parse(_totalRecords.ToString()) / double.Parse(DataGrid1.PageSize.ToString());
			TotalPages.Text = ( System.Math.Ceiling ( _totalPages ) ).ToString();
			
			if ( _currentPageNumber == 1 )
			{
				PreviousPage.Enabled = false;

				if ( _totalPages > 1 )
				{
					NextPage.Enabled = true;
				}
				else
				{
					NextPage.Enabled = false;
				}
			}
			else
			{
				PreviousPage.Enabled = true;

				if ( _currentPageNumber >= (int) _totalPages)
				{
					NextPage.Enabled = false;
				}
				else
				{
					NextPage.Enabled = true; 
				}
			}
		}


		private void ItemsOnPage_SelectedIndexChanged(object sender, EventArgs e)
		{
			int newVal = int.Parse(ItemsOnPage.SelectedValue);
			DisplayItemsCount = newVal;
			DataGrid1.PageSize = DisplayItemsCount;
			BindData();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			ItemsOnPage.SelectedIndexChanged +=new EventHandler(ItemsOnPage_SelectedIndexChanged);
			DataGrid1.ItemCommand +=new DataGridCommandEventHandler(DataGrid1_ItemCommand);
		}
		#endregion

		private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
		{
			int orderId = int.Parse(e.Item.Cells[6].Text);
			BusinessLogic.Order o = new BusinessLogic.Order(orderId);
   			if (e.CommandName == "Accept")
			{
				o.OrderStatus = BusinessLogic.Order.Status.Accepted;
				BusinessLogic.Automation.ActionFactory.RunActionHandlers("accepted",o);
			}
			if (e.CommandName == "Reject")
			{
				o.OrderStatus = BusinessLogic.Order.Status.Rejected;
				BusinessLogic.Automation.ActionFactory.RunActionHandlers("rejected",o);
			}
			BindData();
		}
	}
}
